<?php
class PNTopUserTopicsArray extends PNObjectExArray {
  function PNTopUserTopicsArray($init=null, $where='')
  {
    $this->PNObject();

    $this->_objType       = 'innoforum_topics';
    $this->_objField      = 'id';
    $this->_objPath       = 'form';
    $this->_objWhere      = "tpc_cr_uid <> '2'";
    $this->_objColumnExArray = array( 'is_new'      => "IF ( DATEDIFF(DATE(tpc_cr_date),DATE(NOW())) <1 
                                                             AND
                                                             DATEDIFF(DATE(tpc_cr_date),DATE(NOW())) >-3,
                                                            'T','F')",
                                    'update_new'    => "IF ( DATEDIFF(DATE(tpc_lu_date),DATE(NOW())) <1 
                                                             AND
                                                             DATEDIFF(DATE(tpc_lu_date),DATE(NOW())) >-3,
                                                             'T','F')");
/*
    $this->_objJoin[]     = array ( 'join_table'  =>  'innoforum_type',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('type_name'),
                            'compare_field_table' =>  'type_id',
                            'compare_field_join'  =>  'id');

    $this->_objJoin[]     = array ( 'join_table'  =>  'innoforum_category',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('category_name'),
                            'compare_field_table' =>  'category_id',
                            'compare_field_join'  =>  'id');

    $this->_objJoin[]     = array ( 'join_table'  =>  'innoforum_subcategory',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('subcategory_name'),
                            'compare_field_table' =>  'subcategory_id',
                            'compare_field_join'  =>  'id');

    $this->_objJoin[]     = array ( 'join_table'  =>  'innoforum_posts',
                            'join_field'          =>  array('id'),
                            'object_field_name'   =>  array('post_id'),
                            'compare_field_table' =>  'id',
                            'compare_field_join'  =>  'topics_id');
*/
    $this->_init($init, $where);
  }
  function genSort(){
    $sort  = FormUtil::getPassedValue ('order', false);
    $result = false;
    switch ($sort){
      case 'id':
        $result = 'ORDER BY tpc_id DESC';
        break;
      case 'type':
        $result = 'ORDER BY tpc_type_id';
        break;
      case 'category':
        $result = 'ORDER BY tpc_category_id';
        break;
      case 'subcategory':
        $result = 'ORDER BY tpc_subcategory_id';
        break;
      case 'title':
        $result = 'ORDER BY tpc_title';
        break;
      case 'date':
        $result = 'ORDER BY tpc_cr_date DESC';
        break;
      case 'view':
        $result = 'ORDER BY tpc_count_view DESC';
        break;
      case 'post':
        $result = 'ORDER BY tpc_count_post DESC';
        break;
    }
    if ($result){
      return $result;
    }else{
      //default order
      return 'ORDER BY tpc_count_view DESC,tpc_cr_date DESC';
    }
  
  }

  function genFilter(){
    $filter  = FormUtil::getPassedValue ('filter', false);
    $keyword = DataUtil::formatForStore($filter['keyword']);
    $type = DataUtil::formatForStore($filter['type']);

    $category_id = DataUtil::formatForStore($filter['category_id']);
    $subcategory_id = DataUtil::formatForStore($filter['subcategory_id']);
    $type_id = DataUtil::formatForStore($filter['type_id']);

    $wheres = array();
    //filter by user_id
    $user_id = DataUtil::formatForStore(FormUtil::getPassedValue ('user_id', false));
    if ($user_id) $wheres[] = "tpc_cr_uid = '$user_id'";
    //filter by keyword
    if ($keyword && $type){
      switch($type){
        case 'name': $wheres[] = "tpc_name LIKE '%". $keyword ."%'"; break;
        case 'title': $wheres[] = "tpc_title LIKE '%". $keyword ."%'"; break;
        case 'category_name': $wheres[] = "cty_name LIKE '%". $keyword ."%'"; break;
        case 'subcategory_name': $wheres[] = "scy_name LIKE '%". $keyword ."%'"; break;
      }
    }
    //filter by selector
    if ($category_id && intval($category_id)) $wheres[] = "tpc_category_id ='$category_id'";
    if ($subcategory_id && intval($subcategory_id)) $wheres[] = "tpc_subcategory_id ='$subcategory_id'";
    if ($type_id && intval($type_id)) $wheres[] = "tpc_type_id ='$type_id'";
    
    //except admin and not special topics
    $admin_id = constant('ADMIN_UID');
    $wheres[] = "(tpc_cr_uid <> '$admin_id' OR tpc_obj_status = '1')";
    //show only today
    //$wheres[] = "DATE(tpc_cr_date) = DATE(NOW()) OR DATE(tpc_lu_date) = DATE(NOW())";
    //show only this week
    $wheres[] = "WEEK(tpc_cr_date) = WEEK(NOW()) ";
    $wheres[] = "YEAR(tpc_cr_date) = YEAR(NOW()) ";
    //has image only
    $wheres[] = "tpc_has_image <> 0";

    return implode(' AND ', $wheres) ;
  }
  function selectExtendResult(){
    $user_id = DataUtil::formatForStore(FormUtil::getPassedValue ('user_id', false));
    $result = array();
    if ($user_id){
      $result['user'] = InnoUtil::getUserInfo($user_id);
    }
    return $result;
  }
}